﻿// CNumEdit.cpp: 实现文件
//

#include "stdafx.h"
#include "CNumEdit.h"
#include "MsgBoxDlg.h"


// CNumEdit

IMPLEMENT_DYNAMIC(CNumEdit, CEdit)

CNumEdit::CNumEdit() :
	m_nDotLeftNum(3),
	m_nDotRightNum(0),
	m_dMaxValue(150),
	m_dMinValue(0)
{

}

CNumEdit::~CNumEdit()
{
}

void CNumEdit::SetDotLeftNum(int nDotLeftNum)
{
	ASSERT(nDotLeftNum >= 1);
	m_nDotLeftNum = nDotLeftNum;
	if (CheckNumValid() != 0)
	{
		SetWindowText(L"");
	}
}

int CNumEdit::GetDotLeftNum() const
{
	return m_nDotLeftNum;
}

void CNumEdit::SetDotRightNum(int nDotRightNum)
{
	ASSERT(nDotRightNum >= 0);
	m_nDotRightNum = nDotRightNum;
	if (CheckNumValid() != 0)
	{
		SetWindowText(L"");
	}
}

int CNumEdit::GetDotRightNum() const
{
	return m_nDotRightNum;
}

void CNumEdit::SetMaxValue(double dValue)
{
	ASSERT(dValue >= 0);
	ASSERT(dValue > m_dMinValue);
	m_dMaxValue = dValue;
}

void CNumEdit::SetMinValue(double dValue)
{
	ASSERT(dValue >= 0);
	ASSERT(dValue < m_dMaxValue);
	m_dMinValue = dValue;
}

int CNumEdit::CheckNumValid()
{
	CString str;
	GetWindowText(str);

	int nLen = str.GetLength();
	int nDotPos = str.Find(L".");

	if (nDotPos == 0 && m_nDotRightNum > 0)
	{
		return 2;
	}
	if (nDotPos > m_nDotLeftNum)
	{
		return -1;
	}
	if (m_nDotRightNum > 0 && (nLen - nDotPos - 1) > m_nDotRightNum)
	{
		return 1;
	}

	double dValue = wcstod(str.GetBuffer(), NULL);
	if (m_dMinValue >= 0.0 && m_dMaxValue > m_dMinValue &&
		(dValue < m_dMinValue || dValue > m_dMaxValue))
	{
		return 2;
	}

	return 0;
}


BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
	ON_WM_CHAR()
END_MESSAGE_MAP()



// CNumEdit 消息处理程序




void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if ((nChar == 0x08) || (nChar == 0x10))//,Backspace,Delete
	{
		CEdit::OnChar(nChar, nRepCnt, nFlags);
	}
	CString str, strSel;

	// 获取原来编辑框中的字符串
	GetWindowText(str);
	int nLen = str.GetLength();
	DWORD m = this->GetSel();
	int nRightPos = m >> (sizeof(long) * 4);
	int nLeftPos = m % (1 << sizeof(long) * 4);
	if (nLen >= m_nDotLeftNum + m_nDotRightNum + 1
		&& nRightPos - nLeftPos == 0)
	{
		return;
	}

	int nDotPos = str.Find('.');
	if (nChar == '.')
	{
		if (nDotPos < 0 && m_nDotRightNum > 0 && nLen > 0)
		{
			CEdit::OnChar(nChar, nRepCnt, nFlags);
		}
		else
		{
			return;
		}
	}

	// 除了小数点,还允许输入数字
	if ((nChar >= '0' && nChar <= '9'))
	{
		if ((nDotPos < 0 && nLen < m_nDotLeftNum))
		{
			CEdit::OnChar(nChar, nRepCnt, nFlags);
		}
		else if (nDotPos < 0 && nLen == m_nDotLeftNum && m_nDotRightNum > 0)
		{
			SetWindowText(str + L'.');
			SetSel(nLen + 1, nLen + 1, true);
			CEdit::OnChar(nChar, nRepCnt, nFlags);
		}
		else if ((nDotPos >= 0 && m_nDotRightNum > 0 && (nLen - nDotPos -1) < m_nDotRightNum) || nRightPos - nLeftPos > 0)
		{
			CEdit::OnChar(nChar, nRepCnt, nFlags);
		}
		else
		{
			return;
		}
	}
	else if (nChar == VK_TAB)
	{
		CEdit::OnChar(VK_TAB, nRepCnt, nFlags);
	}
	// 其它的键,都不响应
	else
	{
		return;
	}

	if (CheckNumValid() != 0)
	{
		CString strTip, strFormat;
		strFormat.Format(g_LoadString(L"MSG_EDIT_1"), m_nDotRightNum, m_nDotRightNum);
		strTip.Format(strFormat, m_dMinValue, m_dMaxValue);
		//MessageBox(strTip, L"提示", MB_OK);
		MsgBox(strTip, g_LoadString(L"MSG_EDIT_Prompt"), MB_OK);
		SetWindowText(L"");
		return;
	}

	GetWindowText(str);
	nLen = str.GetLength();
	int nStrLen = m_nDotLeftNum;
	if (m_nDotRightNum > 0)
	{
		if (m_nDotLeftNum > 1)
		{
			nStrLen += m_nDotRightNum + 1;
		}
		else
		{
			nStrLen = m_nDotRightNum + 2;
		}
	}

	if (nStrLen <= nLen)
	{
		//CEdit::OnChar(VK_TAB, nRepCnt, nFlags);
		//SendMessage(WM_KEYDOWN, VK_TAB, 0);
		PostMessage(WM_KEYDOWN, VK_TAB, 0);
	}
	//CEdit::OnChar(nChar, nRepCnt, nFlags);
}
